/** 接口请求封装
 * 参数说明
 * opt:
 * * ****name:具体业务请求名称
 * * ****method:默认POST
 * * ****url:接口路径
 * * ****params或data:具体业务请求参数
 * * ****responseTypeSelf:是否需要单独处理非0000状态的回调函数，1 是需要，false默认不需要直接公用方法处理
 * * ****beforeSend:请求前
 * * ****complete:请求后
 */
// import router from '../../router';
// import urls from '../goUrl.js';
import _axios from './axios';
import $commonJs from '../common.js';
import qs from 'qs';
import Cookie from 'js-cookie';

export default {
	http(opt, callback, callbackFail, callbackNoData, callbackLogin) {
		let opts = opt || {};
		if (!opts.url) {
			return false;
		}
		if (opts.method === undefined) opts.method = 'POST';

		if (opts.beforeSend) opts.beforeSend();

		let contentType = 'application/json; charset=UTF-8';
		let data = JSON.stringify(opts.data);

		if (opts.contentTypeSearch) {
			contentType = 'application/x-www-form-urlencoded; charset=UTF-8';
			data = qs.stringify(opts.data);
		} else if (opts.contentTypeSelf) {
			contentType = opts.contentTypeSelf;
		}
		if (opt.cryptFun) {
			data = JSON.parse(data);
		}
		return _axios({
			method: opts.method,
			url: opts.url,
			params: opts.params || {},
			data: data,
			headers: {
				'content-type': contentType,
				token: opts.token
					? opts.token
					: localStorage.getItem('nb_token')
					? localStorage.getItem('nb_token')
					: Cookie.get('nb_token'),
				'encryption-type': 1,
				customerNo: opts.customerNo || '',
			},
			responseType: opts.responseType && opts.responseType,
			// timeout: 15000,
		})
			.then(
				response => {
					if (opts.complete) opts.complete();
					if (!response) return;
					if (opt.cryptFun) {
						callback(response.data);
						return;
					}
					// 返回图片文件流
					if (opts.responseTypeSelf == 1) {
						if (response.data.status == '4009') {
							// 接口不成功
							let message =
								response?.data?.message || response?.data?.msg || '系统开小差啦，请联系系统管理员';
							callbackFail ? callbackFail(response.data) : $commonJs.toastI(message);
						} else {
							callback && callback(response.data);
						}
					} else {
						// 需要登录的回调
						if (response.data.status == '4007') {
							callbackLogin && callbackLogin(response);
							//   urls.routerPush(this, urls.login, { redirect: router.currentRoute.fullPath }, true);
						} else if (
							response.data.status == '0000' ||
							response.data.code == '0' ||
							response.data.statusCode === 0 ||
							response.data.statusCode === '0' ||
							response.data.status == '4001'
						) {
							callback && callback(response.data);
						} else if (response.data.status == '1000') {
							// 无数据，不用黑条提示，执行callbackNoData
							callbackNoData && callbackNoData(response);
						} else {
							// 接口不成功
							let message =
								response?.data?.message || response?.data?.msg || '系统开小差啦，请联系系统管理员';
							callbackFail ? callbackFail(response.data) : $commonJs.toastI(message);
						}
					}
				},
				response => {
					if (opts.complete) opts.complete();

					let message =
						response?.data?.message || response?.data?.msg || '系统开小差啦，请联系系统管理员';

					callbackFail ? callbackFail(response) : $commonJs.toastI(message);
				},
			)
			.catch(response => {
				if (opts.complete) opts.complete();

				console.error((opts.name || '') + '请求异常', response);
			});
	},
};
